package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private AuditService auditService;
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    //新增申请表单时，会同时新增审批表单数据
    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //申请表单的默认状态为“已发起”
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date()); //设置创建时间
        //新增申请数据入库
        applicationMapper.insert(application);
        //新增审批数据入库
        auditService.insertAudit(application);

    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
       List<ApplicationVO> applicationVOList = applicationMapper.selectApplication(applicationQuery);
       //依次取出ApplicationVO，需要赋值审批人的集合的id和名称
        for (ApplicationVO applicationVO : applicationVOList) {
            //单独将赋值审批人的集合的id和名称封装为一个方法
            auditUserList(applicationVO);
        }
        return applicationVOList;
    }

    @Override
    public void cancel(Long id) {
        //修改对应订单的状态为 20
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //删除该订单关联的所有的审批数据
        auditMapper.deleteByApplicationId(id);
    }

    /**
     * 分配用车
     * @param applicationId
     * @param vehicleId
     */
    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        //申请单数据修改
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        //申请单状态修改为“已分配车辆”
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //车辆数据的修改
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");//2表示占用
        vehicleMapper.update(vehicle);
    }

    /**
     * 还车
     * @param applicationId
     * @param vehicleId
     */
    @Override
    public void back(Long applicationId, Long vehicleId) {
        //申请单数据修改
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        //申请单状态修改为“工单结束”
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //车辆数据的修改
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");//1表示空闲
        vehicleMapper.update(vehicle);
    }

    /**
     * 给applicationVO的auditUserIdList和auditUsernameList进行赋值
     * @param applicationVO
     */
    private void auditUserList(ApplicationVO applicationVO) {
        //准备两个集合，存储审批人的id和姓名
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //根据applicationId查询审批人的信息
        List<AuditVO> auditVOList = auditMapper.selectAuditByApplicationId(applicationVO.getId());
        //遍历auditVOList，取出每一个auditVO对象
        for (int i = 0; i < auditVOList.size(); i++) {
            AuditVO auditVO = auditVOList.get(i);
            //取出当前审批单的id，赋值到auditUserIdList中
            Long auditUserId = auditVO.getAuditUserId();
            auditUserIdList.add(auditUserId);
            //根据审批单的id查询用户数据
            UserVO userVO = userMapper.selectAuditById(auditUserId);
            //将用户的名称赋值到auditUsernameList中
            auditUsernameList.add(userVO.getUsername());
        }
        //将准备好的auditUserIdList赋值到applicationVO中
        applicationVO.setAuditUserIdList(auditUserIdList);
        //auditUsernameList是List类型的，ApplicationVO中的auditUsernameList是String类型的
        //需要借助StringJoiner工具进行转化
        StringJoiner stringJoiner = new StringJoiner(",");
        //lily tom  jack
        for (String username : auditUsernameList) {
            stringJoiner.add(username);//lily,tom,jack
        }
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
